<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 找到小镇的法官 -->
    <script>
      var findJudge = function (n, trust) {
        const inDegrees = new Array(n + 1).fill(0)
        const outDegrees = new Array(n + 1).fill(0)
        for (const edge of trust) {
          const x = edge[0],
            y = edge[1]
          ++inDegrees[y]
          ++outDegrees[x]
        }
        for (let i = 1; i <= n; ++i) {
          if (inDegrees[i] === n - 1 && outDegrees[i] === 0) {
            return i
          }
        }
        return -1
      }
      console.log(findJudge(2, [[1, 2]]))
    </script>
  </body>
</html>
